/**
 * @file childframehome.h
 * @brief
 *
 * 
 *
 * @author 1823419256@qq.com
 * @version 1.0
 * @date 2011
*/

#ifndef MYBOOK_FRAME_CHILDFRAMEHOME_H_
#define MYBOOK_FRAME_CHILDFRAMEHOME_H_

#include "childframe.h"
#include "../webcore/webcore.h"

extern UINT WM_HOME_VIEWCREATED;
DWORD WINAPI _threadHomeCreateView(LPVOID lpData);

class CChildFrameHome
	: public CWindowImpl<CChildFrameHome, CChildFrame, CChildFrameTraits>
	, public CDoubleBufferImpl<CChildFrameHome>
{
public:
	DECLARE_WND_CLASS(_T("MyChildFrameHome"))

	CChildFrameHome() : m_hChildView(NULL)
	{
	}

	HWND CreateFrame(HWND hParent, LPCTSTR lpszUrl)
	{
		m_strUrl = lpszUrl;

		CClientRect rect(hParent);
		return Create(hParent, rect);
	}

	void OnFinalMessage(HWND hWnd)
	{
		delete this;
	}

public:
	BEGIN_MSG_MAP(CChildFrameHome)
		CHAIN_MSG_MAP(CDoubleBufferImpl<CChildFrameHome>)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_SIZE, OnSize)
		MESSAGE_HANDLER(WM_HOME_VIEWCREATED, OnHomeViewCreated)
	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		DWORD dwThreadID;
		CloseHandle(CreateThread(NULL, 0, _threadHomeCreateView, (LPVOID)m_hWnd, NULL, &dwThreadID));

		return 0;
	}

	LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
	{
		if (m_hChildView != NULL)
			::SetWindowPos(m_hChildView, NULL, 0, 0, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), SWP_NOZORDER);

		return 0;
	}

	void DoPaint(CDCHandle dc)
	{
	}

	LRESULT OnHomeViewCreated(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
	{
		m_hChildView = (HWND)lParam;
			
		CClientRect rcClient(m_hWnd);
		::PostMessage(m_hChildView, WM_SIZE, 0, MAKELPARAM(rcClient.Width(), rcClient.Height()));

		CString strUrl;
		if (_tcsnicmp(m_strUrl, _T("http://"), 7) == 0 ||
			_tcsnicmp(m_strUrl, _T("https://"), 8) == 0 ||
			_tcsnicmp(m_strUrl, _T("file://"), 7) == 0)
		{
			strUrl = m_strUrl;
		}
		else
		{
			strUrl.Format(_T("file:///%sdata/%s"), _Module.GetModulePath(), m_strUrl);
			strUrl.Replace(_T("\\"), _T("/"));
		}

		USES_CONVERSION_EX;
		CComBSTR bstrUrl = T2OLE_EX((LPTSTR)(LPCTSTR)strUrl, 0);
		SendMessage(m_hChildView, WM_MYWEB_NAVIGATE, 0, (LPARAM)(BSTR)bstrUrl);

		return 0;
	}

public:
	virtual void Accept(CChildFrameInitVisitor* pVistor)
	{
		pVistor->FrameInitVisit(this);
	}

	virtual bool Equal(CChildFrameTypeVisitor* pVistor)
	{
		return pVistor->FrameTypeVisit(this);
	}

	virtual void DataNotify()
	{
		SendMessage(m_hChildView, WM_MYWEB_REFRESH, 0, 0);
	}

protected:
	HWND m_hChildView;
	CString m_strUrl;
};


// CHomeExternalDispatchAdapter
class CHomeExternalDispatchAdapter : public CExternalDispatch
{
public:
	CHomeExternalDispatchAdapter(CWebContainer* pWebContainer);
	~CHomeExternalDispatchAdapter();

public:
	STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId);
	STDMETHOD(Invoke)(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS  *pDispParams, VARIANT  *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr);
};

// CHomeCustomSiteAdapter
class CHomeCustomSiteAdapter : public CCustomSite
{
public:
	CHomeCustomSiteAdapter(CWebContainer* pWebContainer);
	~CHomeCustomSiteAdapter();

public:
	STDMETHOD(GetExternal)(IDispatch **ppDispatch);
	STDMETHOD(QueryService)(REFGUID guidService, REFIID riid, void **ppvObject);
	STDMETHOD(ShowMessage)(HWND hwnd, LPOLESTR lpstrText, LPOLESTR lpstrCaption, DWORD dwType, LPOLESTR lpstrHelpFile, DWORD dwHelpContext, LRESULT *plResult);

public:
	CExternalDispatch *m_pExternalDispatch;
};

// CHomeWebEventSinkAdapter
class CHomeWebEventSinkAdapter : public CWebEventSink
{
public:
	CHomeWebEventSinkAdapter(CWebContainer* pWebContainer);
	~CHomeWebEventSinkAdapter();

public:
	virtual HRESULT OnNewWindow2(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
	virtual HRESULT OnNewWindow3(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);

protected:
	BOOL IsLocalSnbFile(LPCTSTR lpszUrl);
};

// CHomeWebControlExAdapter
class CHomeWebControlExAdapter : public CWebControlEx
{
public:
	CHomeWebControlExAdapter(CWebContainer* pWebContainer);
	~CHomeWebControlExAdapter();

public:
};

class CHomeAdapterCreator : public CIfaceAdapterCreator
{
public:
	virtual CCustomSite* GetCustomSite(CWebContainer* pWebContainer)
	{
		return new CHomeCustomSiteAdapter(pWebContainer);
	}

	virtual CWebEventSink* GetWebEventSink(CWebContainer* pWebContainer)
	{
		return new CHomeWebEventSinkAdapter(pWebContainer);
	}

	virtual CWebControlEx* GetWebControlEx(CWebContainer* pWebContainer)
	{
		return new CHomeWebControlExAdapter(pWebContainer);
	}
};

#endif // MYBOOK_FRAME_CHILDFRAMEHOME_H_